/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.fritadorcorrente.objetos.dao;

import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author manchini
 */
@Repository
@Transactional
public class GenericoDAO {

    @PersistenceContext
    private  EntityManager em;
    
    public Serializable gravar(Serializable obj) throws DataAccessException{
        return em.merge(obj);
    }
    
    public void excluir(Class classe,Serializable id) throws DataAccessException {
        em.remove(em.find(classe, id));
    }
    
    public Serializable carregar(Class classe,Serializable id) throws DataAccessException{
        return (Serializable) em.find(classe, id);
    }
    
    public List<Serializable> listarTodos(Class classe) throws DataAccessException{
        return em.createQuery("SELECT u FROM "+classe.getName()+" u").getResultList();
    }
    
}
